<?php

namespace app\index\controller;

use app\admin\library\Auth;
use app\common\controller\Frontend;
use EasyWeChat\Factory;
use app\admin\model\Admin;

class Test extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 创建群聊
     * @return string
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\Exception
     */
    public function index()
    {
        $redis = new \Redis();

        $redis->pconnect('127.0.0.1', 6379);//serverip port
        $redis->select(1);
        $i = 0;
        while(true){
            $i++;
            $values = $redis->lPop('form_data');
            if ($values){
                list($vid,$uid)=explode('||',$values);


                $formData=$redis->get('form_'.$vid.'_'.$uid);

                if (!$formData)continue;
                $formData = json_decode($formData,1);
                echo 33;

                $formData['createtime']=time();
                if (!$formData['tableName'])continue;
                $tableName=$formData['tableName'];
                unset($formData['tableName']);
                $fid = Db::name($tableName)->insert($formData,0,1);
                $Round = new \app\admin\model\activtyc\Round;
                $post = [
                    'id'=>$redis->get('nums'),
                    'name'=>$formData['name'],
                    'phone'=>$formData['phone'],
                    'parent_name'=>$formData['parent_name'],
                    // 'type'=>$info['type'],
                    'volumename'=>TableField('Volume','name',$formData['volume_id']),
                    'schoolname'=>TableField('School_B','name',$formData['school_id']),
                    'classname'=>$formData['classname'],
                    'moban'=>$vid==34 ? 'xiaoxue' : 'round'
                ];
//                    $diqu = $Round->getProvinceOrderList();
//                    $shen_text = isset($diqu[$formData['shen']]) ? $diqu[$formData['shen']] : '';
//                    $shi_text = isset($diqu[$formData['shi']]) ? $diqu[$formData['shi']] : '';
//                    $xian_text = isset($diqu[$formData['xian']]) ? $diqu[$formData['xian']] : '';
//                    $post = [
//                        'id'=>$redis->get('nums'),
//                        'name'=>$formData['name'],
//                        'phone'=>$formData['phone'],
//                        'parent_name'=>$formData['parent_name'],
//                        'address'=>$shen_text.$shi_text.$xian_text,
//                        'text'=>$formData['address'],
//                        // 'type'=>$info['type'],
//                        'volumename'=>TableField('Volume','name',$formData['volume_id']),
//                        'schoolname'=>TableField('School_B','name',$formData['school_id']),
//                        'classname'=>$formData['classname'],
//                        'moban'=>$vid==34 ? 'xiaoxue' : 'round'
//                    ];
                $redis->incr('nums');
                $response = \fast\Http::post('http://oa.fc62.com/index/Assistant/chant',$post);
                unset($vid,$uid,$dtdata,$formData,$fid,$diqu,$shen_text,$shi_text,$xian_text,$post,$Round);



                echo 'success'.PHP_EOL;
            }
            if($i > 20000)$i=0;

            if($i%30==0){
                $output->writeln($i.PHP_EOL);
                $values=null;
                usleep(300000);
            }


        }

    }

    public function lists()
    {

    }

    public function updatechat()
    {
        $app = Factory::work(Admin::UserConfig());
        $token = $app->access_token->getToken();//获取企业微信token
        $url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update?access_token={$token['access_token']}&chatid=16721302469901282286";
        $params = [
            'chatid'=>'16012267003567144092',
        ];
        //参数以json格式传送
        $response = \fast\Http::post($url,json_encode($params));
        print_r($response);exit;
    }
    /**
     * 获取群聊会话
     * @throws \EasyWeChat\Kernel\Exceptions\HttpException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function chat_info()
    {
        $app = Factory::work(Admin::UserConfig());
        $token = $app->access_token->getToken();//获取企业微信token
        $url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/get?access_token={$token['access_token']}&chatid=16012267003567144092";
        $response = \fast\Http::get($url);
        print_r($response);exit;
        /**
         * {
        "errcode": 0,
        "errmsg": "ok",
        "chat_info": {
        "chatid": "16721302469901282286",
        "name": "报名表",
        "owner": "yangliu",
        "userlist": [
        "yangliu",
        "ZhangWei",
        "WuJinJun"
        ]
        }
        }
         */
    }

    public function chant()
    {
        $app = Factory::work(Admin::UserConfig());
        $token = $app->access_token->getToken();//获取企业微信token
        $url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token={$token['access_token']}";
        $post = $this->request->post();
        $str = '{
           "chatid": "16721302469901282286",
           "msgtype":"markdown",
           "markdown": {
                "content": "报名表通知
                        >姓名：<font color=\"info\">'.$post['name'].'</font> 
                        >手机号：<font color=\"info\">'.$post['tel'].'</font> 
                        >是否开有书店：<font color=\"info\">'.$post['shudian'].'</font> 
                        >所在地区：<font color=\"info\">'.$post['area'].'</font> 
                        >意向代理地区：<font color=\"info\">'.$post['yixiang'].'</font> 
                        >书店/公司名称：<font color=\"info\">'.$post['mingcheng'].'</font> 
                        >联系方式：<font color=\"info\">'.$post['lianxi'].'</font>"
           }
        }';
        $response = \fast\Http::post($url,$str);
        return ['msg'=>'success'];
    }

}
